<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/12/19
 * Time: 10:33
 */

namespace App\HttpController\Admin;


use App\Model\Categorys\CategorysBean;
use App\Model\Categorys\CategorysModel;

class Category extends Auth
{
    /**
     * 列表
     * @return bool|void
     * @throws \Exception
     */
    public function index(){
        $request = $this->request();
        $data = $request->getRequestParam();
        $page = $data['page'] ?? 1;
        $bean = new CategorysBean(['name'=>$data['name']]);
        $model = new CategorysModel();
        $list = $model->paginate($page,$bean);
        return $this->writeJson(0,$list);
    }

    /**
     * 更新分类列表信息
     * @return bool
     * @throws \Exception
     */
    public function update(){
        $request = $this->request();
        $data = $request->getRequestParam();
        $bean = new CategorysBean($data);
        $model = new CategorysModel();
        if($bean->getId()){
            //修改
            $result = $model->update($bean,$data);
            if($result){
                return $this->writeJson(0,null,'编辑成功');
            }
            return $this->writeJson(1,null,'编辑失败');
        }else{
            //新增
            $result = $model->create($bean);
            if($result){
                return $this->writeJson(0,null,'新增成功');
            }
            return $this->writeJson(1,null,'新增失败');
        }
    }

    /**
     * 删除
     * @return bool
     * @throws \Exception
     */
    public function destroy(){
        $request = $this->request();
        $id = $request->getRequestParam('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $bean = new CategorysBean(['id' => $id]);
        $model = new CategorysModel();
        $res = $model->destroy($bean);
        return $this->writeJson(0,$res);
    }

}